package com.fort.teachermanagementsys.springsecurity.handler;


import com.fort.teachermanagementsys.springsecurity.util.ResultUtil;

import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 登出成功处理类
 * @Author Allen
 * @CreateTime 2024/4/2 10:47
 */
@Component
@Slf4j
public class UserLogoutSuccessHandler implements LogoutSuccessHandler {

    /**
     * 用户登出返回结果
     * 这里应该让前端清除掉Token
     * @Author Allen
     * @CreateTime 2024/4/2 10:50
     */
    @Override
    public void onLogoutSuccess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("登出成功");
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("code", "200");
        resultData.put("msg", "登出成功");
        SecurityContextHolder.clearContext();
        ResultUtil.responseJson((HttpServletResponse) response, ResultUtil.resultSuccess(resultData));
    }
}
